package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.SysCrmFhd;
import com.ruoyi.system.domain.SysCrmFhdDetail;
import com.ruoyi.system.domain.SysCrmKhggc;
import com.ruoyi.system.domain.vo.TimeStrVo;
import com.ruoyi.system.service.*;
import org.aspectj.weaver.loadtime.Aj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.*;

@RestController
@RequestMapping("/system/crm")
public class SysCrmController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(SysCrmController.class);

    @Autowired
    private ISysCrmService sysCrmService;

    @Autowired
    private ISysYongyouService sysYongyouService;

    @Autowired
    private ISysCrmFhdService crmFhdService;

    @Autowired
    private ISysCrmFhdDetailService fhdDetailService;

    /**
     * 用友数据同步
     * @param vo
     * @return
     */

    @PostMapping("/asyncXsht")
    public AjaxResult asyncXsht(@RequestBody TimeStrVo vo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("用友-销售合同[手动]-->开始:"+formattedDate);
        Date start = new Date();
        int count = sysYongyouService.asyncXsht(vo);
        Date end = new Date();
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        long diff = (end.getTime() - start.getTime())/1000;
        System.out.println("用友-销售合同[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "用友-销售合同[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步销售合同 "+count+" 条！总共用时 "+diff+" 秒");
        map.put("end", "用友-销售合同[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }

    @PostMapping("/asyncXsfp")
    public AjaxResult asyncXsfp(@RequestBody TimeStrVo vo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("用友-销售发票[手动]-->开始:"+formattedDate);
        int count = sysYongyouService.asyncXsfp(vo);
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        System.out.println("用友-销售发票[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "用友-销售发票[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步销售发票 "+count+" 条！");
        map.put("end", "用友-销售发票[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }

    @PostMapping("/asyncXsck")
    public AjaxResult asyncXsck(@RequestBody TimeStrVo vo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("用友-销售出库[手动]-->开始:"+formattedDate);
        int count = sysYongyouService.asyncXsck(vo);
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        System.out.println("用友-销售出库[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "用友-销售出库[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步销售出库 "+count+" 条！");
        map.put("end", "用友-销售出库[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }

    @PostMapping("/asyncCprk")
    public AjaxResult asyncCprk(@RequestBody TimeStrVo vo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("用友-其他出库[手动]-->开始:"+formattedDate);
        int count = sysYongyouService.asyncCprk(vo);
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        System.out.println("用友-其他出库[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "用友-其他出库[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步其他出库 "+count+" 条！");
        map.put("end", "用友-其他出库[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }

    @PostMapping("/asyncQtck")
    public AjaxResult asyncQtck(@RequestBody TimeStrVo vo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("用友-产品入库[手动]-->开始:"+formattedDate);
        int count = sysYongyouService.asyncQtck(vo);
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        System.out.println("用友-产品入库[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "用友-产品入库[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步产品入库 "+count+" 条！");
        map.put("end", "用友-产品入库[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }

    @PostMapping("/asyncClck")
    public AjaxResult asyncClck(@RequestBody TimeStrVo vo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("用友-材料出库[手动]-->开始:"+formattedDate);
        int count = sysYongyouService.asyncClck(vo);
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        System.out.println("用友-材料出库[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "用友-材料出库[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步材料出库 "+count+" 条！");
        map.put("end", "用友-材料出库[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }

    @PostMapping("/asyncCgrk")
    public AjaxResult asyncCgrk(@RequestBody TimeStrVo vo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("用友-采购入库[手动]-->开始:"+formattedDate);
        int count = sysYongyouService.asyncCgrk(vo);
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        System.out.println("用友-采购入库[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "用友-采购入库[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步采购入库 "+count+" 条！");
        map.put("end", "用友-采购入库[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }

    @PostMapping("/asyncCgfp")
    public AjaxResult asyncCgfp(@RequestBody TimeStrVo vo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("用友-采购发票[手动]-->开始:"+formattedDate);
        int count = sysYongyouService.asyncCgfp(vo);
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        System.out.println("用友-采购发票[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "用友-采购发票[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步采购发票 "+count+" 条！");
        map.put("end", "用友-采购发票[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }

    @PostMapping("/asyncCgdd")
    public AjaxResult asyncCgdd(@RequestBody TimeStrVo vo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("用友-采购订单[手动]-->开始:"+formattedDate);
        int count = sysYongyouService.asyncCgdd(vo);
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        System.out.println("用友-采购订单[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "用友-采购订单[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步采购订单 "+count+" 条！");
        map.put("end", "用友-采购订单[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }

    /**
     * CRM 数据同步
     * @return
     */
    @PostMapping("/asyncYgxx")
    public AjaxResult asyncYgxx() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("CRM员工信息[手动]-->开始:"+formattedDate);
        int count = sysCrmService.asyncYgxx();
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        System.out.println("CRM员工信息[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "CRM员工信息[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步员工数据 "+count+" 条！");
        map.put("end", "CRM员工信息[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }

    @PostMapping("/asyncKhgl")
    public AjaxResult asyncKhgl(@RequestBody TimeStrVo vo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("CRM客户资料[手动]-->开始:"+formattedDate);
        int count = sysCrmService.asyncKhgl(vo);
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        System.out.println("CRM客户资料[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "CRM客户资料[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步客户数据 "+count+" 条！");
        map.put("end", "CRM客户资料[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }

    @PostMapping("/asyncKhggc")
    public AjaxResult asyncKhggc(@RequestBody TimeStrVo vo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("CRM客户公共池[手动]-->开始:"+formattedDate);
        int count = sysCrmService.asyncKhggc(vo);
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        System.out.println("CRM客户公共池[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "CRM客户公共池[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步客户公共池数据 "+count+" 条！");
        map.put("end", "CRM客户公共池[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }

    @PostMapping("/asyncCpxx")
    public AjaxResult asyncCpxx(@RequestBody TimeStrVo vo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("CRM产品信息[手动]-->开始:"+formattedDate);
        int count = sysCrmService.asyncCpxx(vo);
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        System.out.println("CRM产品信息[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "CRM产品信息[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步产品信息数据 "+count+" 条！");
        map.put("end", "CRM产品信息[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }

    @PostMapping("/asyncHtdd")
    public AjaxResult asyncHtdd(@RequestBody TimeStrVo vo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("CRM合同订单[手动]-->开始:"+formattedDate);
        int count = sysCrmService.asyncHtdd(vo);
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        System.out.println("CRM合同订单[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "CRM合同订单[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步合同订单数据 "+count+" 条！");
        map.put("end", "CRM合同订单[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }

    @PostMapping("/asyncFhd")
    public AjaxResult asyncFhd(@RequestBody TimeStrVo vo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(formatter);
        System.out.println("CRM发货单[手动]-->开始:"+formattedDate);
        int count = sysCrmService.asyncFhd(vo);
        LocalDateTime now1 = LocalDateTime.now();
        String formattedDate1 = now1.format(formatter);
        System.out.println("CRM发货单[手动]-->结束:"+formattedDate1);
        HashMap map = new HashMap();
        map.put("start", "CRM发货单[手动]-->开始:"+formattedDate);
        map.put("data", "成功同步发货单数据 "+count+" 条！");
        map.put("end", "CRM发货单[手动]-->结束:"+formattedDate1);
        return AjaxResult.success(map);
    }
}
